package io.github.annyylxyms.gyn.gateway;

import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 抽象出来一些通用的中间层，处理plugin
 */
public abstract class AbstractGatewayPlugin implements GatewayPlugin {
    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public Mono<Void> handle(ServerWebExchange exchange, GatewayPluginChain chain) {
        boolean supported = support(exchange);
        System.out.println("===>>>>> plugin[" + this.getName() + "],support=" + supported);
        //把handle委托给doHandle
        //return supported ? doHandle(exchange) : Mono.empty();
        //支持的话，就执行doHandle，否则就执行chain的下一个
        return supported ? doHandle(exchange, chain) : chain.handle(exchange);
    }

    @Override
    public boolean support(ServerWebExchange exchange) {
        return doSupport(exchange);
    }

    //handle让每个自定义的实现方法自己去实现，不在这里实现,这里加个抽象的doHandle方法
    public abstract Mono<Void> doHandle(ServerWebExchange exchange, GatewayPluginChain chain);

    public abstract boolean doSupport(ServerWebExchange exchange);
}
